|
|
 |
|
|
|
|
scale_top = pic.ScaleY(pic.ScaleTop, _
pic.ScaleMode, vbPixels)
pix_per_unit_x = pic.ScaleX(1, pic.ScaleMode, vbPixels)
pix_per_unit_y = pic.ScaleY(1, pic.ScaleMode, vbPixels)
' Translate coordinates into pixels. pixel_x = x * pix_per_unit_x - scale_left
pixel_y = y * pix_per_unit_y - scale_top |
|
|
|
|
|
|
|
|
Even with these changes, this method can be time consuming. It can also result in small rounding errors that make objects drawn using Visual Basic methods and objects drawn using API functions fail to line up perfectly. |
|
|
|
|
|
|
|
|
Example program Scale, shown in Figure 2.2, uses the Visual Basic Line method to draw an X on a form. It then converts the coordinates of the lines' endpoints into pixels and uses the Rectangle API function to draw a box around the X. If you look closely, you will see that the lower endpoints of the X do not fall exactly on the bottom edge of the rectangle. |
|
|
|
|
|
|
|
|
Despite these drawbacks, API functions sometimes have advantages over Visual Basic's drawing routines. In fact, because the API functions work only in pixels, they avoid the computations Visual Basic performs to deal with custom coordinate systems. This difference can make the API routines much faster than their Visual Basic counterparts. |
|
|
|
|
|
|
|
|
For instance, Visual Basic's Line method is great for drawing a few dozen lines; but if you need to draw thousands of connected lines all at once, the Polyline API function is much faster. Example program Lines, shown in Figure 2.3, uses the Line method and the Polyline function to draw lines connecting 221 points 100 times. In one test on a 133MHz Pentium, the Polyline function took roughly 1.5 seconds; the Visual Basic Line method needed about 4 seconds. Of course, most programs do not need to draw this many connected line segments. You will have to decide whether the increase in speed is worth the extra trouble for your application. |
|
|
|
|
|
|
|
|
Figure 2.2
Visual Basic coordinates do not
always translate perfectly into pixels. |
|
|
|
|
|